/*******************************************************************************
 **  Name: twi.cpp                                                            **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
#define _MUNIX_TWI_CPP_ID "$Id$"
// Last Modified $DateTime$ by $Author$

#include <util/twi.h>
#include "twi.h"


TWI::TWI(uint8 i, BusMode m)
    : mode(m)
{
    wireSpeed = DEFAULT_TWI_SPEED;
}

void
TWI::Prescaler(uint8 p)
{
    switch(p)
    {
    case 1:
        TWSR &= ~((1<<TWPS1)|(1<<TWPS0));
        break;
    case 4:
        TWSR |= ((1<<TWPS0));
        break;
    case 16:
        TWSR |= (1<<TWPS1);
        break;
    case 64:
        TWSR |= ((1<<TWPS0)|(1<<TWPS1));
        break;
    }
}


bool
TWI::Connect()
{
    BSET(TWCR,TWIE);


    return true;
}

void
TWI::SendByte(uint8 remote, char byte)
{
    //BSET(remote,TW_WRITE);
    StartCondition();
    WaitFlag();
    TWDR = remote;
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    TWDR = byte;
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    StopCondition();
}
void
TWI::SendWord(uint8 remote, wchar_t word)
{
    //BSET(remote,TW_WRITE);
    StartCondition();
    WaitFlag();
    TWDR = remote;
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    TWDR = (word>>8);
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    StartCondition();
    WaitFlag();
    TWDR = remote;
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    TWDR = word;
    TWCR = ((1<<TWINT)|(1<<TWEN));
    WaitFlag();
    StopCondition();
}

// TODO
char
TWI::RecvByte()
{
    return 'z';
}

wchar_t
TWI::RecvWord()
{
    return 'z';
}


